package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 超时自动取消订单
     */
   // @Scheduled(cron = "1/5 * * * * ?")
    public void executeCancelOrder() {
       // log.info("执行自动取消订单任务：{}", new Date());

        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);

        List<Orders> orders = orderMapper.selectBystatus(Orders.PENDING_PAYMENT,time);
        if (orders != null && orders.size() > 0) {
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                orderMapper.update(order);
            }
        }
    }

    /**
     * 每日凌晨1点将派送中订单修改为已完成
     * DELIVERY_IN_PROGRESS
     */
    @Scheduled(cron = "* * 1 * * ?")/**/
    public void executerDeliveryInProgress() {
        log.info("执行自动完成订单任务：{}", new Date());

        LocalDateTime time = LocalDateTime.now().minusHours(1);

        List<Orders> orders = orderMapper.selectBystatus(Orders.DELIVERY_IN_PROGRESS,time);
        if (orders != null && orders.size() > 0) {
            for (Orders order : orders) {
                order.setStatus(Orders.COMPLETED);
                orderMapper.update(order);
            }
        }
    }

}
